﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class frmPresupuestos : System.Web.UI.Page
{
    int a, aux;
   

    protected void Page_Load(object sender, EventArgs e)
    {
        this.DSPresupuesto2.DataSourceMode = SqlDataSourceMode.DataReader;
        SqlDataReader datos = (SqlDataReader)this.DSPresupuesto2.Select(DataSourceSelectArguments.Empty);

        while (datos.Read())
        {
            String dato = datos["nroPresupuesto"].ToString();
            Label2.Text = (Convert.ToInt16(dato) + 1).ToString();        
        }


        LabelDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
       


    }
    

    protected void BotonBuscar_Click(object sender, EventArgs e)
    {

        Label3.ForeColor = System.Drawing.Color.Black; 
        Label3.Text = DropEmpresas.SelectedValue.ToString();
        Label8.Text = Label3.Text;
        Label9.Text = DropEmpresas.SelectedItem.ToString();
        ListBox1.Items.Clear();
        ListBox2.Items.Clear();
        TBPrecio.Text = "";
        TBDias.Text = "";
        TBPrecioTotal.Text = "000";
        GridView2.Visible = false;
        Label5.Text = "";
        DropEstado.BackColor = System.Drawing.Color.FromName("#ccccff");
        GridView2.Visible = true;
        TBVto.CausesValidation = false;
        Label11.Visible = true;
        Label9.Visible = true;

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Label3.Text.Equals("") || Label3.Text.Equals("Debe seleccionar una Empresa"))
        {
            Label3.Text = "Debe seleccionar una Empresa";
            Label3.ForeColor = System.Drawing.Color.Red; 
        }

        else
        {
            TextBox1.Text = "ok";
            aux = Convert.ToInt32(TBPrecioTotal.Text);
            ListBox1.Items.Add(DropServicio.SelectedItem.ToString());
            ListBox2.Items.Add(TBPrecio.Text);
            a = Convert.ToInt32(TBPrecio.Text);
            TBPrecio.Text = "";

            a = a + aux;

            TBPrecioTotal.Text = a.ToString();
        }
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
       
        if (Button3.Text.Equals("Confirmar Cambios"))
        {
            TBDias.Text = "";
            Label10.Text = DropEstado.SelectedValue.ToString();
            int codigo = Convert.ToInt32(Label10.Text);
            this.SqlDataSource4.UpdateParameters["idEstado"].DefaultValue = codigo.ToString();
            SqlDataSource4.Update();

            Response.Redirect("frmPresupuestos.aspx");
        
        }

        else
        {
            

                System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>();
                foreach (ListItem item in ListBox1.Items)
                {
                    items.Add(item.Text.ToString());
                }
                string servicios = String.Join(" | ", items.ToArray());



                this.DSPresupuestos.InsertParameters["cuit"].DefaultValue = Label3.Text;
                this.DSPresupuestos.InsertParameters["detalle"].DefaultValue = servicios;
                this.DSPresupuestos.InsertParameters["importe"].DefaultValue = TBPrecioTotal.Text;
                this.DSPresupuestos.InsertParameters["fechaPresupuesto"].DefaultValue = DateTime.Now.ToString("dd/MM/yyyy");
                this.DSPresupuestos.InsertParameters["idEstado"].DefaultValue = "5";
                this.DSPresupuestos.InsertParameters["fechaValidez"].DefaultValue = TBVto.Text;


                this.DSPresupuestos.Insert();

                this.Label5.Text = "Los Datos se Registraron Correctamente";

                Label3.Text = "";
                ListBox1.Items.Clear();
                ListBox2.Items.Clear();
                TBPrecio.Text = "";
                TBVto.Text = "";
                TBPrecioTotal.Text = "000";
                GridView2.Visible = false;
                 DropEstado.SelectedValue = "1";
                DropServicio.SelectedIndex = 0;
                DropEmpresas.SelectedIndex = 0;

            
        }

    }
    protected void TodosPresupuestos_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmListaDePresupuestos.aspx");
    }
    
    protected void Button5_Click(object sender, EventArgs e)
    {

        //int dias = DateTime.Today.DayOfYear;
        DateTime inicio = Convert.ToDateTime(LabelDate.Text);

        int dias2 =Convert.ToInt16(TBDias.Text);
        DateTime final = inicio.AddDays(Convert.ToInt32(dias2));
          
        string dat = final.ToString("dd/MM/yyyy");

        TBVto.Text = dat.ToString();

    }
    protected void Button4_Click(object sender, EventArgs e)
    {

        Label3.Text = "";
        TBPrecio.Text = "";
        ListBox1.Items.Clear();
        ListBox2.Items.Clear();
        TBPrecio.Text = "";
        TBPrecioTotal.Text = "000";
        GridView2.Visible = false;
        DropEstado.SelectedValue = "1";
        TBVto.Text = "";
       

        DropEstado.BackColor = System.Drawing.Color.FromName("#ccccff");
        DropEmpresas.Enabled = true;
        TBVto.ReadOnly = false;
        Label3.Enabled = true;
        ListBox1.Enabled = true;
        TBPrecioTotal.Enabled = true;
        DropServicio.Enabled = true;
        TBPrecio.ReadOnly = false;
        ListBox2.Enabled = true;
        BotonBuscar.Enabled = true;
        Button5.Enabled = true;
        Button2.Enabled = true;
        TBDias.ReadOnly = false;


    }
    protected void Button6_Click(object sender, EventArgs e)
    {
        Response.Redirect("MenuServicios.aspx");

    }
    protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
    {

        Button3.Text="Confirmar Cambios";
        Label13.Visible = true;
        DropEstado.Visible = true;
        
        Label7.Text = this.GridView2.Rows[this.GridView2.SelectedIndex].Cells[1].Text;

        this.SqlDataSource3.SelectParameters["nroPresupuesto"].DefaultValue = this.Label7.Text;
        this.SqlDataSource3.DataSourceMode = SqlDataSourceMode.DataReader;
        SqlDataReader datos = (SqlDataReader)this.SqlDataSource3.Select(DataSourceSelectArguments.Empty);

        if (datos.Read())
        {

            ListBox1.Items.Clear();
            Label2.Text = datos["nroPresupuesto"].ToString();
            DropEstado.SelectedValue = datos["idEstado"].ToString();
            DropEmpresas.SelectedValue = datos["cuit"].ToString();
            TBVto.Text = datos["fechaValidez"].ToString();
            Label3.Text = datos["cuit"].ToString();
            ListBox1.Items.Add(datos["detalle"].ToString());
            TBPrecioTotal.Text = datos["importe"].ToString();
            //TBVto.Text = datos["fechaValidez"].ToString();
            TBVto.Text = "000";
           

            DropEstado.BackColor = System.Drawing.Color.Magenta;
            DropEmpresas.Enabled = false;
            TBVto.ReadOnly = true;
            Label3.Enabled = false;
            ListBox1.Enabled = false;
            TBPrecioTotal.Enabled = false;
            DropServicio.Enabled = false;
            TBPrecio.ReadOnly = true;
            ListBox2.Enabled = false;
            BotonBuscar.Enabled = false;
            Button5.Enabled = false;
            Button2.Enabled = false;
            TBDias.ReadOnly = true;


        }

    }


    protected void TBDias_TextChanged(object sender, EventArgs e)
    {
        if (TBDias.Text.Equals(""))
            Label26.Visible = true;

        else
        {
            DateTime inicio = Convert.ToDateTime(LabelDate.Text);

            int dias2 = Convert.ToInt16(TBDias.Text);
            DateTime final = inicio.AddDays(Convert.ToInt32(dias2));

            string dat = final.ToString("dd/MM/yyyy");

            TBVto.Text = dat.ToString();
        }

    }
}